package com.example.swaggerproject.controller;

import com.example.swaggerproject.entity.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@Tag(name = "用户管理", description = "用户的增删改查操作")
public class UserController {

    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "成功", content = {@Content(mediaType = "application/json")}),
            @ApiResponse(responseCode = "400", description = "请求参数有误"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @GetMapping("/{id}")
    @Operation(summary = "根据ID查询用户信息", description = "返回单个用户对象")
    public Result<String> findById(@Parameter(description = "用户ID唯一标识", required = true) @PathVariable Long id) {
        if (id == 1L) {
            return Result.success("用户id为：" + id);
        } else {
            return Result.error("用户id为：" + id);
        }

    }

    @PostMapping("/")
    @Operation(summary = "创建用户", description = "创建一个新的用户")
    public Result<String> createUser() {
        return Result.success("创建用户成功");
    }

    @Parameters({
            @Parameter(name = "name", description = "用户名", in = ParameterIn.QUERY),
            @Parameter(name = "age", description = "用户年龄", in = ParameterIn.QUERY)
    })
    @GetMapping("/")
    public Result<String> getUser(@RequestParam("name") String name, @RequestParam("age") Integer age) {
        if (!name.isEmpty() && age > 0) {
            return Result.success("用户名：" + name + "，年龄：" + age);
        } else {
            return Result.error("用户名：" + name + "，年龄：" + age);
        }
    }

}
